<template>
	<view style="background-color: #F3F3F3;">
		<view style="padding:10px;background-color: #FFFFFF;">
			<u-form ref="uForm" labelWidth="80px;">
				<u-form-item label="联系人">
					<u-input v-model="address.name" placeholder="请输入您的称呼" />
				</u-form-item>
				<u-form-item label="性别">
					<u-radio-group v-model="address.gender">
						<u-radio
						      :customStyle="{marginBottom: '8px'}"
						      label="先生"
						      name="先生"
						    >
						</u-radio>
						<u-radio
						      :customStyle="{marginBottom: '8px'}"
						      label="女士"
						      name="女士"
						    >
						</u-radio>
					</u-radio-group>
				</u-form-item>
				<u-form-item label="手机号">
					<u-input v-model="address.phone" />
					<u-button size="mini" type="success" slot="right" style="font-size:10px;" open-type="getPhoneNumber"
						@getphonenumber="getPhoneNumber">自动获取
					</u-button>
				</u-form-item>
				<u-form-item label="地址" @click="toUrl('/pages/mine/address-choose')">
					<u-input v-model="address.address" :disabled="true" placeholder="选择收货地址" />
				</u-form-item>
				<u-form-item label="门牌号">
					<u-input v-model="address.room" placeholder="如:3号楼318室"/>
				</u-form-item>
				<!-- <u-form-item label="默认地址">
					<u-switch slot="right" v-model="address.isDefault"></u-switch>
				</u-form-item> -->
			</u-form>
		</view>
		<view style="width:100%;padding:10px;position: fixed;bottom:10px;">
			<u-button type="primary" shape="circle" @click="submit">保存</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				address: {
					name: '',
					phone: '',
					address: '',
					gender: '先生',
					choosedAdd: '',
					room: ''
				},
			}
		},
		onShow(){
			this.request.get(`api/user`,{"@expands": "['entity.profile']"}).then((res) => {
				if(res.data.profile.__metadata.nickname != null && res.data.profile.__metadata.nickname != '' && res.data.profile.__metadata.nickname != '微信用户' && this.address.name == ''){
					this.address.name = res.data.profile.__metadata.nickname
				}
				if(res.data.profile.__metadata.phone != null && res.data.profile.__metadata.phone != ''){
					this.address.phone = res.data.profile.__metadata.phone
				}
			})
			//获取缓存选择的地址
			let choosedAdd = uni.getStorageSync('chooseAdd')
			this.address.address = choosedAdd.title
			this.address.choosedAdd = choosedAdd
			uni.removeStorageSync('chooseAdd')
		},
		methods: {
			getPhoneNumber(e) {
				if (typeof(e.detail.iv) == 'undefined') {
					uni.showToast({
						title: '获取失败,请手动输入',
						icon: 'none'
					})
				} else {
					let _this = this;
					uni.login({
						provider: "weixin",
						success: function(res) {
							let data = {
								code: res.code
							}
							_this.request.get("wechat/mini/login", data).then((res) => {
								uni.setStorageSync('api_token', res.data.token);
								if (res.data.data.session_key) {
									let data = {
										sessionKey: res.data.data.session_key,
										iv: e.detail.iv,
										encryptedData: e.detail.encryptedData,
									}
									_this.request.post(`wechat/decrypt-data`, data).then((res) => {
										_this.address.phone = res.data.phoneNumber
										console.log('phone', res);
										//更新手机号码
										_this.request.put(`api/user-profile`, {
											phone: res.data.phoneNumber
										}).then((res) => {
											uni.hideLoading()
										})
									})
								} else {
									uni.hideLoading()
									uni.showToast({
										title: '获取手机号码失败！',
										icon: 'none'
									})
								}
							})
						},
					});
				}
			},
			toUrl(page){
				uni.navigateTo({
					url: page
				})
			},
			submit() {
				let address = this.address
				if(address.name == ''  || address.phone == '' || address.address == '' ){
					uni.showToast({
						title: '请填写相关内容！',
						icon: 'none'
					})
					return
				}
				let myAddresses = uni.getStorageSync('myAddresses')
				if(myAddresses == ''){
					myAddresses = [address]
				}
				else{
					myAddresses.push(address)
				}
				uni.setStorageSync('myAddresses',myAddresses)
				uni.navigateTo({
					url: '/pages/mine/addresses'
				})
			}
		}
	}
</script>

<style>
	.time {
		color: #909399;
		font-size: 13px;
	}

	.add {
		color: #18B566;
		font-weight: bold;
		font-size: 16px;
	}

	.dev {
		color: #909399;
		font-weight: bold;
		font-size: 16px;
	}

	.wrap {
		padding: 10px 0px
	}
</style>
